#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "stdint.h"
#include "stdbool.h"
#include "driver/gpio.h"
#include "lcd_device.h"
#include "math.h"  
#include "string.h"

static const char *TAG = "lcd_device";
spi_device_handle_t lcd_spi_handle;

unsigned short lcd_width = 240;         //lcd的宽度
unsigned short lcd_height = 320;        //lcd的高度
unsigned char lcd_orientation = LCD_DISPLAY_ORIENTATION_PORTRAIT; //lcd默认纵向显示

// 发送命令到LCD，使用轮询方式阻塞等待传输完成。
// 由于数据传输量很少，因此在轮询方式处理可提高速度。使用中断方式的开销要超过轮询方式。
static void lcd_write_cmd(const unsigned char cmd)
{
	esp_err_t ret;
	spi_transaction_t t;

	memset(&t, 0, sizeof(t));		// 清空结构体
	t.length = 8;						// 要传输的位数 一个字节 8位
	t.tx_buffer = &cmd;				// 将命令填充进去
	t.user = (void*)0;				// 设置D/C 线，在SPI传输前回调中根据此值处理DC信号线
	ret = spi_device_polling_transmit(lcd_spi_handle, &t);		// 开始传输
	assert(ret==ESP_OK);			// 一般不会有问题
}

// 发送数据到LCD，使用轮询方式阻塞等待传输完成。
// 由于数据传输量很少，因此在轮询方式处理可提高速度。使用中断方式的开销要超过轮询方式。
static void lcd_write_data(const unsigned char *data, int len)
{
	esp_err_t ret;
	spi_transaction_t t;

	if (len==0) return;				// 长度为0 没有数据要传输

	memset(&t, 0, sizeof(t));		// 清空结构体

	t.length = len * 8;					// 要写入的数据长度 Len 是字节数，len, transaction length is in bits.
	t.tx_buffer = data;				// 数据指针
	t.user = (void*)1;				// 设置D/C 线，在SPI传输前回调中根据此值处理DC信号线
	ret = spi_device_polling_transmit(lcd_spi_handle, &t);		// 开始传输
	assert(ret==ESP_OK);			// 一般不会有问题
}
// 发送数据到LCD，使用轮询方式阻塞等待传输完成。
// 由于数据传输量很少，因此在轮询方式处理可提高速度。使用中断方式的开销要超过轮询方式。
void lcd_write_data_16(unsigned short data)
{
	esp_err_t ret;
	spi_transaction_t t;
	uint8_t dataBuf[2] = {0,0};

	dataBuf[0] = data >> 8;
	dataBuf[1] = data & 0xFF;
	memset(&t, 0, sizeof(t));		// 清空结构体
	t.length = 2 * 8;					// 要写入的数据长度 Len 是字节数，len, transaction length is in bits.
	t.tx_buffer = dataBuf;				// 数据指针
	t.user = (void*)1;				// 设置D/C 线，在SPI传输前回调中根据此值处理DC信号线
	ret = spi_device_polling_transmit(lcd_spi_handle, &t);		// 开始传输

	assert(ret == ESP_OK);			// 一般不会有问题
}

// 此函数在SPI传输开始之前被调用（在irq上下文中！），通过用户字段的值来设置D/C信号线
static void lcd_spi_pre_transfer_callback(spi_transaction_t *t)
{
	int dc = (int)t -> user;

	gpio_set_level(PIN_NUM_DC, dc);
}

static void send_line_finish(spi_device_handle_t spi)
{
	spi_transaction_t *rtrans;
	esp_err_t ret;

	//Wait for all 6 transactions to be done and get back the results.
	for (int x = 0; x < 6; x++) 
	{
		ret = spi_device_get_trans_result(spi, &rtrans, portMAX_DELAY);
		assert(ret == ESP_OK);
		//We could inspect rtrans now if we received any info back. The LCD is treated as write-only, though.
	}
}


/* To send a set of lines we have to send a command, 2 data bytes, another command, 2 more data bytes and another command
 * before sending the line data itself; a total of 6 transactions. (We can't put all of this in just one transaction
 * because the D/C line needs to be toggled in the middle.)
 * This routine queues these commands up as interrupt transactions so they get
 * sent faster (compared to calling spi_device_transmit several times), and at
 * the mean while the lines for next transactions can get calculated.
 */
static void send_lines(spi_device_handle_t spi, int ypos, uint16_t *linedata)
{
	esp_err_t ret;
	int x;
	//Transaction descriptors. Declared static so they're not allocated on the stack; we need this memory even when this
	//function is finished because the SPI driver needs access to it even while we're already calculating the next line.
	static spi_transaction_t trans[6];

	//In theory, it's better to initialize trans and data only once and hang on to the initialized
	//variables. We allocate them on the stack, so we need to re-init them each call.
	for (x = 0; x < 6; x++) 
	{
		memset(&trans[x], 0, sizeof(spi_transaction_t));
		if ((x & 1) == 0) 
		{
			//Even transfers are commands
			trans[x].length = 8;
			trans[x].user = (void*)0;
		} 
		else 
		{
			//Odd transfers are data
			trans[x].length = 8*4;
			trans[x].user = (void*)1;
		}
		trans[x].flags = SPI_TRANS_USE_TXDATA;
	}

	trans[0].tx_data[0] = 0x2A;           //Column Address Set
	trans[1].tx_data[0] = 0;              //Start Col High
	trans[1].tx_data[1] = 0;              //Start Col Low
	trans[1].tx_data[2] = (320) >> 8;       //End Col High
	trans[1].tx_data[3] = (320) & 0xff;     //End Col Low
	trans[2].tx_data[0] = 0x2B;           //Page address set
	trans[3].tx_data[0] = ypos >> 8;        //Start page high
	trans[3].tx_data[1] = ypos & 0xff;      //start page low
	trans[3].tx_data[2] = (ypos + PARALLEL_LINES) >> 8;    //end page high
	trans[3].tx_data[3] = (ypos + PARALLEL_LINES) & 0xff;  //end page low
	trans[4].tx_data[0] = 0x2C;           //memory write
	trans[5].tx_buffer = linedata;        //finally send the line data
	trans[5].length = 320*2*8*PARALLEL_LINES;          //Data length, in bits
	trans[5].flags = 0; //undo SPI_TRANS_USE_TXDATA flag

	//Queue all transactions.
	for (x = 0; x < 6; x++) 
	{
		ret = spi_device_queue_trans(spi, &trans[x], portMAX_DELAY);
		assert(ret == ESP_OK);
	}

	//When we are here, the SPI driver is busy (in the background) getting the transactions sent. That happens
	//mostly using DMA, so the CPU doesn't have much to do here. We're not going to wait for the transaction to
	//finish because we may as well spend the time calculating the next line. When that is done, we can call
	//send_line_finish, which will wait for the transfers to be done and check their status.
}



// 获取屏幕驱动芯片ID
unsigned int lcd_get_id(void)
{	
	spi_transaction_t t;

	lcd_write_cmd(0x04);  // 获取屏幕驱动芯片ID指令 0x04
	
	memset(&t, 0, sizeof(t));

	t.length = 8*3;
	t.flags = SPI_TRANS_USE_RXDATA;
	t.user = (void*)1;

	esp_err_t ret = spi_device_polling_transmit(lcd_spi_handle, &t);

	assert(ret == ESP_OK);

	return *(unsigned int*)t.rx_data;
}

// 设置屏幕方向
void lcd_set_orientation(unsigned char orientation)
{
	const char *orientation_str[] = {"PORTRAIT", "PORTRAIT_INVERTED", "LANDSCAPE", "LANDSCAPE_INVERTED"};
	uint8_t data[] = {(1<<3)|(0<<6)|(0<<7), (1<<3)|(1<<6)|(1<<7), (1<<3)|(0<<7)|(1<<6)|(1<<5), (1<<3)|(1<<7)|(1<<5)};

	printf("%s->Display orientation: %s\n",TAG, orientation_str[orientation]);
	
	//(1<<3)|(0<<6)|(0<<7));		// BGR==1,MY==0,MX==0,MV==0
	//(1<<3)|(0<<7)|(1<<6)|(1<<5));	// BGR==1,MY==1,MX==0,MV==1
	//(1<<3)|(1<<6)|(1<<7));		// BGR==1,MY==0,MX==0,MV==0
	//(1<<3)|(1<<7)|(1<<5));		// BGR==1,MY==1,MX==0,MV==1
	lcd_orientation = orientation;

	if(orientation == LCD_DISPLAY_ORIENTATION_PORTRAIT || orientation == LCD_DISPLAY_ORIENTATION_PORTRAIT_INVERTED)
	{
		lcd_width = 240;		// LCD 宽度
		lcd_height = 320;		// LCD 高度
	}
	else if(orientation == LCD_DISPLAY_ORIENTATION_LANDSCAPE || orientation == LCD_DISPLAY_ORIENTATION_LANDSCAPE_INVERTED)
	{
		lcd_width = 320;		// LCD 宽度
		lcd_height = 240;		// LCD 高度
	}

	printf("%s->0x36 command value: 0x%02X\n",TAG, data[orientation]);
	lcd_write_cmd(0x36);
	lcd_write_data((void *) &data[orientation], 1);
}

// 清屏指定颜色
void lcd_clear(unsigned short color)
{
	unsigned int i,m;  
	unsigned char databuf[2] = {0,0};

	lcd_set_windows(0,0,lcd_width-1,lcd_height-1); 

	for(i = 0;i < lcd_height;i++)
	{
		for(m = 0;m < lcd_width;m++)
		{	
			databuf[0] = (color >> 8) & 0xFF;
			databuf[1] = color & 0xFF;
			lcd_write_data(databuf,2);
		}
	}
} 

// 设置窗口
void lcd_set_windows(unsigned short x_star, unsigned short y_star,unsigned short x_end,unsigned short y_end)
{	
	unsigned char databuf[4] = {0,0,0,0};

	databuf[0] = x_star >> 8;
	databuf[1] = 0xFF & x_star;	
	databuf[2] = x_end >> 8;
	databuf[3] = 0xFF & x_end;

	lcd_write_cmd(0x2A);
	lcd_write_data(databuf,4);

	databuf[0] = y_star >> 8;
	databuf[1] = 0xFF & y_star;	
	databuf[2] = y_end >> 8;
	databuf[3] = 0xFF & y_end;

	lcd_write_cmd(0x2B);	
	lcd_write_data(databuf,4);

	lcd_write_cmd(0x2C);			
}   

// 设置光标位置
void lcd_set_cursor(unsigned short x_pos, unsigned short y_pos)
{
	lcd_set_windows(x_pos,y_pos,x_pos,y_pos);	
} 


void lcd_device_init(void)
{
	int cmd = 0;
	esp_err_t ret;

	//ili9341的初始化命令参数
	lcd_init_cmd_t ili_init_cmds[] = {
		{0xCF, {0x00, 0xD9, 0X30}, 3},
		{0xED, {0x64, 0x03, 0X12, 0X81}, 4},
		{0xE8, {0x85, 0x10, 0x7A}, 3},
		{0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5},
		{0xF7, {0x20}, 1},
		{0xEA, {0x00, 0x00}, 2},
		{0xC0, {0x1B}, 1},          /*Power control*/
		{0xC1, {0x12}, 1},          /*Power control */
		{0xC5, {0x26, 0x26}, 2},    /*VCOM control*/
		{0xC7, {0xB0}, 1},          /*VCOM control*/
		{0x36, {0x08}, 1},          /*Memory Access Control*/
		{0x3A, {0x55}, 1},			/*Pixel Format Set*/
		{0xB1, {0x00, 0x1A}, 2},
		{0xB6, {0x0A, 0xA2}, 2},
		{0xF2, {0x00}, 1},	
		{0x26, {0x01}, 1},	
		{0xE0, {0x1F, 0x24, 0x24, 0x0D, 0x12, 0x09, 0x52, 0XB7, 0x3F, 0x0C, 0x15, 0x06, 0x0E, 0x08, 0x00}, 15},
		{0XE1, {0x00, 0x1B, 0x1B, 0x02, 0x0E, 0x06, 0x2E, 0x48, 0x3F, 0x03, 0x0A, 0x09, 0x31, 0x37, 0x1F}, 15},
		{0x2B, {0x00, 0x00, 0x01, 0x3f}, 4},
		{0x2A, {0x00, 0x00, 0x00, 0xEF}, 4},	
		//{0x2C, {0}, 0},
		//{0xB7, {0x07}, 1},
		{0x11, {0}, 0x80},
		{0x29, {0}, 0x80},
		{0, {0}, 0xff},
	};

	//spi总线配置参数
	spi_bus_config_t buscfg = {
		.miso_io_num = PIN_NUM_MISO,
		.mosi_io_num = PIN_NUM_MOSI,
		.sclk_io_num = PIN_NUM_CLK,
		.quadwp_io_num = -1,
		.quadhd_io_num = -1,
		.max_transfer_sz=PARALLEL_LINES*320*2+8
	};

	// SPI驱动接口配置
	spi_device_interface_config_t devcfg = {
		.clock_speed_hz = 30*1000*1000,				// SPI时钟 30 MHz
		.mode = 0,									// SPI模式0
		.spics_io_num = PIN_NUM_CS,					// CS片选信号引脚
		.queue_size = 7,							// 事务队列尺寸 7个
		.pre_cb = lcd_spi_pre_transfer_callback,	// 数据传输前回调，用作D/C（数据命令）线分别处理
	};

	// 初始化SPI总线
	ret = spi_bus_initialize(LCD_HOST, &buscfg, DMA_CHAN);ESP_ERROR_CHECK(ret);

	// 添加SPI总线驱动
	ret = spi_bus_add_device(LCD_HOST, &devcfg, &lcd_spi_handle);ESP_ERROR_CHECK(ret);

	// 初始化其它控制引脚
	gpio_set_direction(PIN_NUM_DC, GPIO_MODE_OUTPUT);
	//gpio_set_direction(PIN_NUM_RST, GPIO_MODE_OUTPUT);
	//gpio_set_direction(PIN_NUM_BCKL, GPIO_MODE_OUTPUT);

	// Reset the display
	//gpio_set_level(PIN_NUM_RST, 0);
	//vTaskDelay(100 / portTICK_RATE_MS);
	//gpio_set_level(PIN_NUM_RST, 1);
	//vTaskDelay(100 / portTICK_RATE_MS);

	uint32_t lcd_id = lcd_get_id();
	ESP_LOGI(TAG, "LCD ID: %08X\n", lcd_id);

	printf("LCD ILI9341 initialization.\n");

	// 循环发送设置所有寄存器
	while (ili_init_cmds[cmd].databytes!=0xff) 
	{
		lcd_write_cmd(ili_init_cmds[cmd].cmd);
		lcd_write_data(ili_init_cmds[cmd].data, ili_init_cmds[cmd].databytes&0x1F);
		if (ili_init_cmds[cmd].databytes&0x80) {
			vTaskDelay(100 / portTICK_RATE_MS);
		}
		cmd++;
	}
    lcd_set_orientation(LCD_DISPLAY_ORIENTATION_PORTRAIT);// 纵向

	//LCD_WriteCMD(0x21);// 翻转颜色
	lcd_write_cmd(0x20);
	lcd_clear(WHITE);
	// gpio_set_level(PIN_NUM_BCKL, 0);	// 点亮LCD屏

}
